//data 是数据 | tapdata 点击的数据  | operate  del 删除  add 添加
const recursionFn = (data,tapdata,operate) => {
	// 使用map遍历,生成新的数组
	let delindex = -1;
	console.info("data",data);
	
	data = data.map((item,index) => {
		if(tapdata.item_id==item.id){
			console.info("找到了要操作的那个！",index);
			if(operate=='add'){
				let numid = Math.ceil(Math.random()*1000);
				console.info(numid,"numid");
				if(item.child){
					// let 
					item.child.push({name: numid,place:"boy",id:numid})
				}else{
					item.child = [{name: numid,place:"boy",id:numid}]
				}
			}else if(operate=='del'){
				//删除
				console.info("删除----------",index);
				delindex = index;
				// data = data.splice(index,1)
			}
		}
		if(item.child){
			item.child = recursionFn(item.child,tapdata,operate)
		}
		return item;
		// return {
		//     // ...item, // 如果想在原数组添加属性
		//     name: item.label,
		//     id: item.value,
		//     children: item.children ? Fn(item.children) : item.children // 判断当前是否还有子节点
		// }
	})
	if(delindex!=-1){
		// data = data.splice(delindex-1,1);
		console.info("删除",delindex);
		// data = 
		data.splice(delindex,1);
	}
	// console.info(delindex,"delindex");
	delindex = -1;
	return data
}


export default {
	state: {
		treeData:[],
	},
	mutations: {
		//修改数据
		async updateTreeItem(state, item) {
			let {
				key,
				val
			} = item;
			state[key] = val;
		}
	},
	actions: {
		//添加子级
		addChild({
			commit,
			state
		}, param){
			console.info("param add",param,state.treeData);
			let newsdata = recursionFn(state.treeData,param,'add');
			commit("updateTreeItem",{key:"treeData",val:newsdata});
		},
		//删除子级
		delChild({
			commit,
			state
		}, param){
			console.info("param 删除子级",param);
			let newsdata = recursionFn(state.treeData,param,'del');
			commit("updateTreeItem",{key:"treeData",val:newsdata});
		},
		///判断是否是顶级 没有父辈录入
		add_father({commit,state},param){
			return new Promise((ret,rej)=>{
				let bool = false;
				for(let i = 0;i<state.treeData.length;i++){
					if(state.treeData[i].id==param.id){
						bool = true;
						console.info(item.id,param.id);
					}
				}
				if(bool){
					ret(bool)
				}else{
					ret(bool)
				}
			})
		}
		
	}
}
